package com.hrt.manage.comments.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hrt.base.BaseController;
import com.hrt.bean.Comments;
import com.hrt.bean.Replys;
import com.hrt.bean.UsersInfor;
import com.hrt.manage.comments.service.CommentsService;
import com.hrt.manage.comments.service.ReplysService;
import com.hrt.manage.member.service.HRTMemberService;
import com.hrt.manage.product.service.GiftDetailService;
import com.hrt.vo.CommentsVO;
import com.hrt.vo.ReplysVO;

@Controller
@RequestMapping("manage/comments/replys")
public class ReplyController extends BaseController<Replys>{
	//评论
	@Autowired
	private CommentsService commentsService;
	
	//礼品基本信息
	@Autowired
	private GiftDetailService giftDetailService;
	
	//会员
	@Autowired
	private HRTMemberService hRTMemberService;
	
	//回复
	@Autowired
	private ReplysService replysService;
	
	/*private List<Comments> commentsList;
	private Comments comments;
	private Replys replys;*/
	
	/**根据评论的ID找到回复的列表*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/queryReply")
	@ResponseBody
	public Map queryReply(
			@RequestParam(value = "id", required = true) Long commentsId,
			HttpServletRequest request, HttpServletResponse response) {
		Map resultMap = new HashMap();
		List<Replys> resultList = this.replysService.getReplysBycommId(commentsId);
		resultMap.put("resultList", resultList);
		return resultMap;
	}
	
	/**
	 * 加载一个礼品实体
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/loadReplys")
	@ResponseBody
	public Map loadReplys(
			@RequestParam(value = "id", required = true) String commId,
			HttpServletRequest request, HttpServletResponse response) {
		Map map = new HashMap();
		if (commId != null && !"".equals(commId)) {
			//获取评论的实体
			Comments comments = this.commentsService.getById(new Long(commId));
			ReplysVO replysVO = new ReplysVO();
			//评论
			replysVO.setComments(comments);
			replysVO.setCommentsId(comments.getId());
			//会员
			replysVO.setHrtMember(comments.getHrtMember());
			replysVO.setHrtMemberId(comments.getHrtMemberId());
			replysVO.setHrtMemberName(comments.getHrtMemberName());
			//礼品规格
			replysVO.setGiftDetail(comments.getGiftDetail());
			replysVO.setGdeId(comments.getGdeId());
			replysVO.setGdeName(comments.getGdeName());
			if(comments != null){
				map = transBean2Map(replysVO);
			}
		}
		return map;
	}
	
	
	/**
	 * 删除回复评论
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/delItemById")
	@ResponseBody
	public boolean delItemById(
			@RequestParam(value = "id", required = true) Long itemId,
			HttpServletRequest request, HttpServletResponse response) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		if (itemId != null && !"".equals(itemId)) {
			if (itemId != null) {
				return replysService.delItemById(itemId);
			}
		}
		return false;
	}
	
	/**
	 * 添加修改回复(注入上一级commentsVO)
	 * */
	@RequestMapping(value = "/saveReply", method = RequestMethod.POST)
	public void saveReply(
			@ModelAttribute("giftTypeDir") CommentsVO commentsVO,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<ReplysVO> replysVO = new ArrayList<ReplysVO>();
		if(commentsVO.getReplysVO() !=null){
			for(ReplysVO repVO : commentsVO.getReplysVO()){
				if(repVO.getId() == null && repVO.getCommentsId() != null){
					repVO.setIsdelete(0);
					
					//获取登录用户
					UsersInfor uInfor = this.getOnlineManager(request, response);
					//设置用户
					repVO.setUsersInfor(uInfor);
					//设置用户id
					repVO.setUsersInforId(uInfor.getId());
					//设置用户名
					repVO.setUsersInforName(uInfor.getLoginName());
					
					replysVO.add(repVO);
				}
				if(repVO.getId() != null && repVO.getCommentsId() != null){
					//获取登录用户
					UsersInfor uInfor = this.getOnlineManager(request, response);
					//设置用户
					repVO.setUsersInfor(uInfor);
					//设置用户id
					repVO.setUsersInforId(uInfor.getId());
					//设置用户名
					repVO.setUsersInforName(uInfor.getLoginName());
					replysVO.add(repVO);
				}
			}
			this.replysService.updateReplys(replysVO);
		}
		response.sendRedirect("/HRTWeb/manage/comments/list.do");
	}
	
	
}
